package com.hethongviet.vending.helper;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.widget.TextView;

import com.hethongviet.vending.AppMain;
import com.hethongviet.vending.Constant;
import com.google.android.DemoKit.R;
import com.hethongviet.vending.Utils;

public class TimerOutUtils
{

	private Timer _timerTimeNotOperating = null;
	private Timer _timerTimeOutUser = null;
	private Timer _timerTimeOutDialog = null;
	private long _timeOutRemain = 0;
	private TextView _textViewTimeOut = null;
	private final Activity _activity;
	private final long _timeOut;
	private final long _timeNotOperating;

	public TimerOutUtils(Activity activity, int textTimeResource,
			boolean isUserMode)
	{
		this._activity = activity;
		_textViewTimeOut = (TextView) activity.findViewById(textTimeResource);
		if (isUserMode)
		{
			_timeOut = Constant.TIME_OUT_USER;
			_timeNotOperating = Constant.TIME_OUT_USER_NOT_OPERATING;
		}
		else
		{
			_timeOut = Constant.TIME_OUT_SERVICE;
			_timeNotOperating = Constant.TIME_OUT_SERVICE_NOT_OPERATING;
		}
	}

	public void stop()
	{
		stopTimerOutUser();
		stopTimerNotOperating();
	}

	private long _timePause = 0;

	public void pause()
	{
		_timePause = _timeOutRemain;
		stop();
	}

	public void resume()
	{
		startTimerOutUser();
		_timeOutRemain = _timePause;
		startTimerNotOperating();
	}

	public void stopTimerOutUser()
	{
		if (_timerTimeOutUser != null)
		{
			_timerTimeOutUser.cancel();
			_timerTimeOutUser = null;
		}
	}

	public void startTimerOutUser()
	{
		stopTimerOutUser();
		TimerTask timerTask = new TimerTask() {

			@Override
			public void run()
			{
				_timeOutRemain -= Constant.TIME_OUT_STEP;

				if (_timeOutRemain <= 0)
				{
					displayMessageTimeOut(R.string.message_time_out_user);
					cancel();
				}
				else changeTimeView(_timeOutRemain);
			}
		};
		_timeOutRemain = _timeOut;
		_timerTimeOutUser = new Timer();
		_timerTimeOutUser.scheduleAtFixedRate(timerTask, 0,
				Constant.TIME_OUT_STEP);

	}

	private void changeTimeView(final long timeRemain)
	{
		_activity.runOnUiThread(new Runnable() {

			@Override
			public void run()
			{
				_textViewTimeOut.setText(getStringTimeShow(timeRemain));
			}
		});
	}

	public void stopTimerNotOperating()
	{
		if (_timerTimeNotOperating != null)
		{
			_timerTimeNotOperating.cancel();
			_timerTimeNotOperating = null;
		}
	}

	public void startTimerNotOperating()
	{
		stopTimerNotOperating();

		TimerTask timerTask = new TimerTask() {

			@Override
			public void run()
			{
				displayMessageTimeOut(R.string.message_time_out_user_not_operating);
				cancel();
			}
		};

		_timerTimeNotOperating = new Timer();
		_timerTimeNotOperating.schedule(timerTask, _timeNotOperating);

	}

	private String getStringTimeShow(long timeMillis)
	{
		int secondTimes = (int) (timeMillis / 1000);
		String time = "";

		int hour = (int) (secondTimes / (60 * 60));
		String hourString = "" + (hour > 9 ? hour : ("0" + hour));
		time += hourString;

		int minite = (int) (secondTimes / (60));
		String miniteString = ":" + (minite > 9 ? minite : ("0" + minite));
		time += miniteString;

		int second = (int) (secondTimes % (60));
		String secondString = ":" + (second > 9 ? second : ("0" + second));
		time += secondString;

		return time;
	}

	public void startTimeOutDialog()
	{
		startTimeOutDialog(Constant.TIME_OUT_WAIT_DIALOG);
	}

	public void startTimeOutDialog(long time)
	{
		stopTimeOutDialog();

		TimerTask timerTask = new TimerTask() {

			@Override
			public void run()
			{
				cancel();
				_activity.finish();
			}
		};

		_timerTimeOutDialog = new Timer();
		_timerTimeOutDialog.schedule(timerTask, time);

	}

	private void displayMessageTimeOut(final int messageRecource)
	{

		_activity.runOnUiThread(new Runnable() {

			@Override
			public void run()
			{
				startTimeOutDialog();

				_activity.setResult(Activity.RESULT_OK);

				// _activity.runOnUiThread(new Runnable() {
				//
				// @Override
				// public void run()
				// {
				// _activity.showDialog(messageRecource);
				// }
				// });
				if (messageRecource == R.string.message_time_out_user_not_operating)
				{
					AppMain.getInstance().setCodeResult(CodeResult.TIME_OUT_NOT_OPERATOR);
				}
				else
				{
					AppMain.getInstance().setCodeResult(CodeResult.TIME_OUT_USER);
				}
				
				Utils.debug("Call onWindowFocusChange!!!!!!!!");
				_activity.onWindowFocusChanged(true);
			}
		});
	}

	public void stopTimeOutDialog()
	{
		if (_timerTimeOutDialog != null)
		{
			_timerTimeOutDialog.cancel();
			_timerTimeOutDialog = null;
		}
	}

}
